#include <QtSql>
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "defaultsettings.h"

SettingsDialog::SettingsDialog(QWidget *parent, QTranslator *translator) :
    QDialog(parent),
    ui(new Ui::SettingsDialog)
{
    ui->setupUi(this);
    getCurrentSettings();
    fillFormWithCurrentSettings();
    ui->tabWidget->setCurrentIndex(0);
    this->translator = translator;
    applyInstantNow = false;
}

SettingsDialog::~SettingsDialog()
{
    delete ui;
}

void SettingsDialog::getCurrentSettings()
{
    DefaultSettings def;
    currentSettingsList = def.getDefaultSettings("ALL");
    int numberofsettings = currentSettingsList.count();
    QSqlQuery query("SELECT * from settings");
    while (query.next()) {
        QStringList settingfromDB;
        settingfromDB << query.record().value("Pref_Category").toString()
                   << query.record().value("Pref_Key").toString()
                   << query.record().value("Pref_Value").toString();
        settingIsInDB.append(settingfromDB);
        for (int i = 0; i < numberofsettings; ++i) {
            if (currentSettingsList.at(i).at(1) == settingfromDB.at(1)) {
                currentSettingsList[i][2] = settingfromDB.at(2);
                break;
            }
        }
    }
}

void SettingsDialog::fillFormWithCurrentSettings()
{
    foreach (QStringList onesetting, currentSettingsList) {
        if (onesetting.at(0) == "MAIN") {
            if (onesetting.at(1) == "GUI_Language") {
                if (onesetting.at(2) == "en")
                    ui->language_English_radio->setChecked(true);
                else if (onesetting.at(2) == "hu")
                    ui->language_Hungarian_radio->setChecked(true);
            }
            else if (onesetting.at(1) == "GUI_Stylesheet") {
                switch (onesetting.at(2).toInt()) {
                case 0:
                    ui->radioButton_Style0->setChecked(true);
                    break;
                case 1:
                    ui->radioButton_Style1->setChecked(true);
                    break;
                case 2:
                    ui->radioButton_Style2->setChecked(true);
                    break;
                case 3:
                    ui->radioButton_Style3->setChecked(true);
                    break;
                case 4:
                    ui->radioButton_Style4->setChecked(true);
                    break;
                }
            }
        }
        if (onesetting.at(0) == "MIXER") {
            if (onesetting.at(1) == "Use_Grouping") {
                if (onesetting.at(2) == "on")
                    ui->checkBox_UseGrouping->setChecked(true);
                else
                    ui->checkBox_UseGrouping->setChecked(false);
            }
            else if (onesetting.at(1) == "Show_Hungarian") {
                if (onesetting.at(2) == "on")
                    ui->checkBox_ShowHungarian->setChecked(true);
                else
                    ui->checkBox_ShowHungarian->setChecked(false);
            }
            else if (onesetting.at(1) == "Right_Click_Translate") {
                if (onesetting.at(2) == "on")
                    ui->checkBox_UseRightClickButtons->setChecked(true);
                else
                    ui->checkBox_ShowHungarian->setChecked(false);
            }
        }
    }
}

void SettingsDialog::collectNewSettings()
{
    foreach (QStringList onesetting, currentSettingsList) {
        QString newvalue;
        if (onesetting.at(0) == "MAIN") {
            if (onesetting.at(1) == "GUI_Language") {
                if (ui->language_English_radio->isChecked())
                    newvalue = "en";
                else if (ui->language_Hungarian_radio->isChecked())
                    newvalue = "hu";
                if (onesetting.at(2) != newvalue)
                    newSettingsList.append(QStringList() << onesetting[0] << onesetting[1] << newvalue);
            }
            else if (onesetting.at(1) == "GUI_Stylesheet") {
                if (ui->radioButton_Style0->isChecked())
                    newvalue = "0";
                else if (ui->radioButton_Style1->isChecked())
                    newvalue = "1";
                else if (ui->radioButton_Style2->isChecked())
                    newvalue = "2";
                else if (ui->radioButton_Style3->isChecked())
                    newvalue = "3";
                else if (ui->radioButton_Style4->isChecked())
                    newvalue = "4";
                if (onesetting.at(2) != newvalue)
                    newSettingsList.append(QStringList() << onesetting[0] << onesetting[1] << newvalue);
            }
        }
        if (onesetting.at(0) == "MIXER") {
            if (onesetting.at(1) == "Use_Grouping") {
                if (ui->checkBox_UseGrouping->isChecked())
                    newvalue = "on";
                else
                    newvalue = "off";
                if (onesetting.at(2) != newvalue)
                    newSettingsList.append(QStringList() << onesetting[0] << onesetting[1] << newvalue);
            }
            else if (onesetting.at(1) == "Show_Hungarian") {
                if (ui->checkBox_ShowHungarian->isChecked())
                    newvalue = "on";
                else
                    newvalue = "off";
                if (onesetting.at(2) != newvalue)
                    newSettingsList.append(QStringList() << onesetting[0] << onesetting[1] << newvalue);
            }
            else if (onesetting.at(1) == "Right_Click_Translate") {
                if (ui->checkBox_UseRightClickButtons->isChecked())
                    newvalue = "on";
                else
                    newvalue = "off";
                if (onesetting.at(2) != newvalue)
                    newSettingsList.append(QStringList() << onesetting[0] << onesetting[1] << newvalue);
            }
        }
    }
}

bool SettingsDialog::checkSettingIsInDB(QString key)
{
    foreach (QStringList onesetting, settingIsInDB) {
        if (onesetting.at(1) == key)
            return true;
    }
    return false;
}

void SettingsDialog::saveSettings()
{
    collectNewSettings();
    foreach (QStringList onesetting, newSettingsList) {
        QSqlQuery query;
        if (checkSettingIsInDB(onesetting.at(1)))
            query.prepare("UPDATE settings SET Pref_Value=:value WHERE Pref_Category=:categ AND Pref_Key=:key");
        else
            query.prepare("INSERT INTO settings VALUES (:categ, :key, :value)");
        query.bindValue(":categ", onesetting.at(0));
        query.bindValue(":key", onesetting.at(1));
        query.bindValue(":value", onesetting.at(2));
        query.exec();
        if (applyInstantNow) {
            if (onesetting.at(1) == "GUI_Language") {
                QString language(":/translations/");
                language.append(onesetting.at(2));
                translator->load(language);
                qApp->installTranslator(translator);
                ui->retranslateUi(this);
            }
            if (onesetting.at(1) == "GUI_Stylesheet") {
                QFile qss("stylesheets/style_" + onesetting.at(2) + ".qss");
                if (!qss.open(QIODevice::ReadOnly | QIODevice::Text)) {
                    qss.setFileName(":/stylesheets/style_" + onesetting.at(2) + ".qss");
                    qss.open(QIODevice::ReadOnly | QIODevice::Text);
                }
                qApp->setStyleSheet(qss.readAll());
                qss.close();
            }
        }
    }
    applyInstantNow= false;
}

void SettingsDialog::on_pushButton_Apply_clicked()
{
    applyInstantNow = true;
    saveSettings();
    currentSettingsList.clear();
    settingIsInDB.clear();
    newSettingsList.clear();
    getCurrentSettings();
}

void SettingsDialog::on_pushButton_RestoreDefaults_clicked()
{
    QSqlQuery query("DELETE from settings");
    query.clear();
    currentSettingsList.clear();
    settingIsInDB.clear();
    getCurrentSettings();
    fillFormWithCurrentSettings();
}
